<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>

<%@ page import="com.engine.taskmask.persistence.PMF"%>
<%@ page import="com.engine.taskmask.persistence.iTask"%>
<%@ page import="com.engine.taskmask.persistence.Statistics"%>
<%@ page import="com.engine.taskmask.sync.IDate"%>
<%@ page import="javax.jdo.Query"%>
<%@ page import="java.lang.Integer"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Task Mask, Application Web Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<style type="text/css">

/* Sticky footer styles
      -------------------------------------------------- */
html,body {
	height: 100%;
	/* The html and body elements cannot have any padding or margin. */
}

/* Wrapper for page content to push down footer */
#wrap {
	min-height: 100%;
	height: auto !important;
	height: 100%;
	/* Negative indent footer by it's height */
	margin: 0 auto -60px;
}

/* Set the fixed height of the footer here */
#push,#footer {
	height: 60px;
}

#footer {
	background-color: #f5f5f5;
}

/* Lastly, apply responsive CSS fixes as necessary */
@media ( max-width : 767px) {
	#footer {
		margin-left: -20px;
		margin-right: -20px;
		padding-left: 20px;
		padding-right: 20px;
	}
}

/* Custom page CSS
      -------------------------------------------------- */
/* Not required for template or sticky footer method. */
.container {
	width: auto;
	max-width: 680px;
}

.container .credit {
	margin: 20px 0;
}
</style>

<!-- Le styles -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
	padding-top: 60px;
	/* 60px to make the container go all the way to the bottom of the topbar */
}
</style>

<link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet">

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="../assets/js/html5shiv.js"></script>
    <![endif]-->


<script type="text/javascript">

	/* delete servlete submit' post etmek icin..*/
	function deleteTask(){
		document.getElementById('DeleteForm').submit();
	}
	
</script>

</head>

<body>
	<div class="navbar ">
		<div class="navbar-inner">
			<div class="container">
				<button type="button" class="btn btn-navbar" data-toggle="collapse"
					data-target=".nav-collapse">
					<span class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a class="brand" href="#">Task Mask</a>
				<div class="nav-collapse collapse">
					<ul class="nav">
						<li class="active"><a href="index.html">Home</a></li>
						<li><a href="about.html">About</a></li>
						<li><a href="contact.html">Contact</a></li>
					</ul>
					<ul class="nav pull-right">
						<li class="dropdown"><a href="#" class="dropdown-toggle"
							data-toggle="dropdown">Welcome, <%
							UserService userService = UserServiceFactory.getUserService();
							User user = userService.getCurrentUser();
							if (user != null) {
						%> <%=user.getNickname()%> <%
 	} else {
 %> <%=" Guest"%> <%
 	response.sendRedirect("/index.jsp");
 	}
 %> <b class="caret"></b></a>
							<ul class="dropdown-menu">

								<li><a href=""><i class="icon-cog"></i> Preferences</a></li>
								<li><a href="/contact.html"><i class="icon-envelope"></i>
										Contact Support</a></li>
								<li class="divider"></li>
								<li><a
									href="<%=userService.createLogoutURL(request.getRequestURI())%>"><i
										class="icon-off"></i> Logout</a></li>
							</ul></li>
					</ul>
				</div>
				<!--/.nav-collapse -->
			</div>
		</div>
	</div>
	<%
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(iTask.class);

		query.setFilter("user_id == id");
		query.declareParameters("String id");
		List<iTask> tasks = (List<iTask>) query.execute(user.getUserId());
		
		int i = 0;

		if (tasks.isEmpty()) {
	%>

	<div class="container">
		<div class="alert alert-info">
			<button type="button" class="close" data-dismiss="alert">&times;</button>
			<h4>No Tasks.</h4>
		</div>
	</div>
	<%
		} else {
	%>
	<div class="container">

		<div class="btn-toolbar">
			<button class="btn btn-primary">New Task</button>
		</div>
		<form class="form-search" id="searchform" method="get"
			action="/listview.jsp">
			<div class="input-append">
				<input type="text" name="searchKey">
				<button type="submit" class="btn">Search</button>
			</div>
		</form>
		<div class="page-header">
			<%
				String searchKey = request.getParameter("searchKey");
				
					// arama yok
					if (searchKey == null)
						out.write("Tasks List:");
					else{
						// arama var.
						out.write("Search results:");
						
						// filtreleme yapalım..
						
						for(Iterator itr = tasks.iterator(); itr.hasNext(); ){
							
							// yeni bir task persistent nesnesi yarat
							iTask task = (iTask) itr.next();
							
							// arama içeriyormu bak.
							//icermiyorsa sil.
							if(!task.containsField(searchKey)){
								itr.remove();
							}
						}	
					}
			%>
		</div>

		<form name="deleteTaskForm" id="DeleteForm" action="/deleteTask"
			method="post">

			<table
				class="table table-striped table-hover table-bordered table-condensed ">

				<thead>
					<tr>
						<th>#</th>
						<th>Title</th>
						<th>Category</th>
						<th>Date</th>
						<th>Edit-Del</th>
					</tr>
				</thead>
				<tbody>
					<%
					for (iTask task : tasks) {
							i++;
				%>
					<tr>
						<td><%=i%></td>
						<td><%=task.getTaskTitle()%></td>
						<td><%=task.getTaskCategory()%></td>
						<td><%=task.getDate_s()%></td>
						<td><a href=<%="/taskview.jsp?id=" + pm.getObjectId(task)%>><i
								class="icon-pencil"></i>  </a> <input type="checkbox"
							id="<%="delete" + i%>" name="<%="delete" + i%>"
							value="<%=task.getUnique()%>"></td>
					</tr>
					<%
					}
				%>
				</tbody>

			</table>
			<div class="control-group">
				<label class="control-label"></label>
				<div class="controls">
					 <a href="/Export"><button class="btn btn-info">Export</button></a>
					 <input type="submit" value="Delete" class="btn btn-danger">
				</div>
			</div>
				
		</form>
		<%
			}

			query.closeAll();
			pm.close();
		%>
	</div>

	<div id="footer">
		<div class="container">
			<p class="muted credit">
				All rights reserved <a href="http://ahmetoz.com.tr">Ahmet ÖZ</a> and
				<a href="#"> Cansu Aras</a> // 2013 - Ege University.
			</p>
		</div>
	</div>

	<!-- Le javascript
    ================================================== -->
	<!-- Placed at the end of the document so the pages load faster -->

	<script src="bootstrap/js/jquery.js"></script>
	<script src="bootstrap/js/bootstrap.min.js"></script>

</body>
</html>



